package com.ude.pim.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ude.pim.common.R;
import com.ude.pim.dto.CaseSubDelayApplyDTO;
import com.ude.pim.dto.CaseSubFinishApplyDTO;
import com.ude.pim.entity.CaseSub;
import com.ude.pim.entity.CaseSubDelayApply;
import com.ude.pim.entity.CaseSubFinishApply;
import com.ude.pim.mapper.CaseSubFinishApplyMapper;
import com.ude.pim.service.CaseSubFinishApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ude.pim.service.CaseSubService;
import com.ude.pim.utils.PIMConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yb
 * @since 2023-08-25
 */
@Service
public class CaseSubFinishApplyServiceImpl extends ServiceImpl<CaseSubFinishApplyMapper, CaseSubFinishApply> implements CaseSubFinishApplyService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CaseSubService caseSubService;

    /**
     * 1. 首先判断是否有已经在审核中的完结申请
     * 2. 有，则拒绝，否则，添加到数据库
     * @param caseSubFinishApply
     * @return
     */
    @Override
    public R<String> applyFinish(CaseSubFinishApply caseSubFinishApply) {
        Long caseSubId = caseSubFinishApply.getCaseSubId();
        LambdaQueryWrapper<CaseSubFinishApply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CaseSubFinishApply::getCaseSubId,caseSubId);
        lqw.eq(CaseSubFinishApply::getStatus,0);
        CaseSubFinishApply one = this.getOne(lqw);
        if(one!=null)
            return R.error("该阶段的完结申请已经提交，可能提交自其他负责人，您无需重复提交");

        caseSubFinishApply.setStatus(0);
        this.save(caseSubFinishApply);
        return R.success("申请提交成功");
    }

    @Override
    public R<List<CaseSubFinishApplyDTO>> checkList() {
        LambdaQueryWrapper<CaseSubFinishApply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CaseSubFinishApply::getStatus,0);
        List<CaseSubFinishApply> list = this.list(lqw);
        return R.success(map2DTO(list));
    }

    private List<CaseSubFinishApplyDTO> map2DTO(List<CaseSubFinishApply> list) {
        return list.stream().map(s->{
            CaseSubFinishApplyDTO dto = new CaseSubFinishApplyDTO();
            BeanUtils.copyProperties(s,dto);

            //专案名
            String nameJson = stringRedisTemplate.opsForValue().get("caseSubId2Name:" + s.getCaseSubId());
            Map<String, String> map = JSONUtil.toBean(nameJson, Map.class);
            dto.setCaseName(map.get("caseName"));
            dto.setSubName(map.get("subName"));
            //申请人姓名
            String applyName = stringRedisTemplate.opsForValue().get("userId2Name:" + s.getApplyId());
            dto.setApplyName(applyName);

            return dto;

        }).collect(Collectors.toList());
    }

    /**
     * 1. 通过审核==>赋值状态，审核时间，审核人
     * 2. 删除对应的redis子流程缓存
     * 3. 给caseSub表中的对应子流程完结时间进行赋值
     * 4. 自动开始下一个阶段
     * 5. 拒绝审核==>赋值状态，审核时间，审核人，拒绝理由
     * @param caseSubFinishApplyDTO
     * @return
     */
    @Override
    public R<String> judge(CaseSubFinishApplyDTO caseSubFinishApplyDTO) {
        Integer status = caseSubFinishApplyDTO.getStatus();
        //通过
        if(status==1){
            //2. 删除对应的redis专案缓存
            stringRedisTemplate.delete(PIMConstants.CASE_SUB_LIST+caseSubFinishApplyDTO.getCaseSubId());

            //3. 给caseSub表中的对应子流程完结时间进行赋值
            CaseSub caseSub = caseSubService.getById(caseSubFinishApplyDTO.getCaseSubId());
            caseSub.setFinishTime(this.getById(caseSubFinishApplyDTO.getId()).getCreateTime());
            caseSubService.updateById(caseSub);

            //4. 自动开始下一个阶段
            //caseSubService.startNext(caseSubFinishApplyDTO.getCaseSubId());
        }
        //拒绝
        caseSubFinishApplyDTO.setCheckTime(LocalDateTime.now());
        this.updateById(caseSubFinishApplyDTO);
        return R.success("审核成功");
    }

}
